<template>
  <div class="departments-container">
    <div class="app-container">
      <!-- 卡片组件 -->
      <el-card class="box-card">
        <!-- 使用 Tabs 组件完成标签页布局 -->
        <el-tabs v-model="activeName">
          <el-tab-pane label="组织架构" name="first" class="tab-pane">
            <!-- 使用 Layout 布局绘制头部区域 -->
            <el-row type="flex" justify="space-between" align="middle" class="department-header">
              <el-col :span="20">
                <i class="el-icon-s-home" />
                <span class="company">江苏传智播客教育科技股份有限公司</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <el-col>负责人</el-col>
                  <el-col>
                    <el-dropdown>
                      <span class="el-dropdown-link">
                        操作<i class="el-icon-arrow-down el-icon--right" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="chanShow('',1)">添加子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
            <!-- 主体区域绘制 -->
            <el-tree class="departments-tree" :data="treeData" :props="defaultProps" :default-expand-all="true">
              <template slot-scope="{ data }">
                <el-row type="flex" justify="space-between" style="height: 50px; width: 100%;" align="middle">
                  <el-col :span="20">
                    <!-- <i v-if="!data.children" class="el-icon-s-custom" /> -->
                    <span>{{ data.name }}</span>
                  </el-col>
                  <el-col :span="4">
                    <el-row type="flex" justify="end">
                      <el-col>{{ data.manager }}</el-col>
                      <el-col>
                        <el-dropdown>
                          <span class="el-dropdown-link">
                            操作<i class="el-icon-arrow-down el-icon--right" />
                          </span>
                          <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item @click.native="chanShow(data.id,1)">添加子部门</el-dropdown-item>
                            <el-dropdown-item @click.native="chanShow(data.id,2)">编辑部门</el-dropdown-item>
                            <el-dropdown-item v-if="data &&!data.children" @click.native="removeDepartment(data.id)">删除部门</el-dropdown-item>
                          </el-dropdown-menu>
                        </el-dropdown>
                      </el-col>
                    </el-row>
                  </el-col>
                </el-row>
              </template>
            </el-tree>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 弹出框 -->
    <el-dialog :title="num===1?'添加子部门':'编辑部门'" :visible.sync="outerVisible">
      <deptDialog v-if="outerVisible" :pid="pid" :department="DepartmentList" :num="num" @changeShow="changeShowFn" @addDep="addDepFn" />
    </el-dialog>
  </div>
</template>
<script>
import deptDialog from './deptDialog.vue'
import { getDepartmentList, removeDepartment } from '@/api/department'
import { changeTreeFn } from '@/utils/changeTree'
export default {
  components: {
    deptDialog
  },
  data() {
    return {
      activeName: 'first',
      treeData: [],
      outerVisible: false,
      DepartmentList: [],
      originList: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      pid: '',
      num: 1
    }
  },
  async created() {
    await this.getDepartmentListFn()
  },
  methods: {
    async getDepartmentListFn() {
      const res = await getDepartmentList()
      this.DepartmentList = res.data.depts
      this.treeData = changeTreeFn(res.data.depts, '')
      this.originList = res.data.depts.map(item => {
        ({
          id: item.id,
          name: item.name,
          code: item.code,
          pid: item.pid
        })
      })
    },
    chanShow(pid, num) {
      this.outerVisible = true
      this.pid = pid
      this.num = num
    },
    // 删除部门
    removeDepartment(id) {
      this.$confirm('是否删除这条记录?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await removeDepartment(id)
        await this.getDepartmentListFn()
        this.$message.success('删除成功')
      })
    },
    changeShowFn(bool) {
      this.outerVisible = bool
    },
    async addDepFn(bool) {
      await this.getDepartmentListFn()
      this.outerVisible = bool
    }
  }

}
</script>

<style lang="scss" scoped>
.box-card {
  padding: 10px 30px;
}

.tab-pane {
  padding: 20px 80px;

  .company {
    margin-left: 12px;
    font-weight: bold;
    font-size: 14px;
  }

  .department-header {
    height: 50px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
  }
}
.departments-tree {
  margin: 12px 0px;
}
</style>
